package helper;

import java.awt.Image;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.ImageIcon;

public class BasicHelper {
	Random random;

	/**
	 * Method to create random boolean.
	 * @return random boolean
	 */
	public boolean getRandomBoolean() {

		random = new Random();
		int min, max;
		min = 0;
		max = 1;

		if (getIntegerBetween(min, max) == 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Method to create random customer y entry position.
	 * @return random y position
	 */
	public int getRandomYPosition() {
		int a1[] = { 220,475 };

		random = new Random();
		int min, max;
		min = 0;
		max = a1.length - 1;

		int randomNum = random.nextInt(max - min + 1) + min;
		return a1[randomNum];

	}

	/**
	 * Method to create random customer X entry position.
	 * @return random x position
	 */
	public int getRandomXPosition() {
		int a1[] = { 1, 25, 50, 75, 100, 125, 150, 250, 275,
				300, 325, 350, 375, 400 };

		random = new Random();
		int min, max;
		min = 0;
		max = a1.length - 1;

		int randomNum = random.nextInt(max - min + 1) + min;
		return a1[randomNum];

	}

	/**
	 * Method to generate a list with sprite images.
	 * @param string determines which customer sprites are used
	 * @param LeftSpriteList sprite list for customer walking left to right
	 * @param RightSpriteList sprite list for customer walking right to left
	 */
	public void createSpriteList(String string,ArrayList<Image> LeftSpriteList, ArrayList<Image> RightSpriteList) {
		ArrayList<Image> leftWalk = LeftSpriteList;
		for (int i = 0; i < 6; i++) {
			ImageIcon i1 = new ImageIcon(this.getClass().getResource(
					"/images/" + string + "/left_to_right/" + i + ".png"));
			Image image1 = i1.getImage();
			leftWalk.add(image1);
		}

		ArrayList<Image> rightWalk = RightSpriteList;
		for (int i = 0; i < 6; i++) {
			ImageIcon i1 = new ImageIcon(this.getClass().getResource(
					"/images/" + string + "/right_to_left/" + i + ".png"));
			Image image1 = i1.getImage();
			rightWalk.add(image1);
		}

	}

	/**
	 * Method to get random double value.
	 * @param low minimum double 
	 * @param high maximum double
	 * @return random double amount
	 */
	public double getRandomAmount(int low, int high) {
		double amount = Math.random() * (high - low) + low;
		return roundTwoDecimalPlaces(amount);
	}
	
	/**
	 * Method to round the incoming double value and get a new value with two decimal places.
	 * @param amount not formated
	 * @return formated amount
	 */
	public double roundTwoDecimalPlaces(double amount){
		double temp = amount * 100;
		temp = Math.round(temp);
		temp = temp / 100;
		return temp;
		
	}
	
	/**
	 * Method to generte a integer value between min and max values
	 * @param min minimum integer
	 * @param max maximum integer
	 * @return random integer
	 */
	public int getIntegerBetween(int min,int max) {
		random = new Random();
		int randomNum = random.nextInt(max - min + 1) + min;
		return randomNum;
	}
	
	/**
	 * Method to get the first element of specific object from list.
	 * @param list with objects
	 * @param clazz specific object class
	 * @return first specific object element
	 */
	public static <T> T getFirstElementOfTypeIn( List<?> list, Class<T> clazz )
	{
	  for ( Object o : list )
	  {
	    if ( o != null && o.getClass() == clazz  )
	    {
	      return clazz.cast( o );
	    }
	  }
	  return null;
	}
	
	/**
	 * Method to check if element exists.
	 * @param list with elements
	 * @param clazz object class
	 * @return boolean if exists
	 */
	public <T> boolean doesElementOfTypeExistIn( List<?> list, Class<T> clazz )
	{
	  for ( Object o : list )
	  {
	    if ( o != null && o.getClass() == clazz)
	    {
	      return true;
	    }
	  }
	  return false;
	}

}
